package com.neu.config;


import com.neu.base.ServerSentEventsResult;
import com.neu.enums.ServerSentEventsEnum;
import com.neu.handler.ServerSentEventsManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SseHeartbeat {

    /**
     * 保持SSE连接，定时向客户端发送数据
     * 通过 Spring 的 @Scheduled 定期执行
     */
    @Scheduled(fixedRate = 60000) // 每60秒发送一次心跳
    public void keepHeartbeat () {
        String name = ServerSentEventsEnum.SSE_HEART_BEAT.name();
        log.info("Sending heartbeat: {}", name);
        ServerSentEventsManager.send(new ServerSentEventsResult<>(ServerSentEventsEnum.SSE_HEART_BEAT, name));
    }
}
